﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using System.Data.Common;

namespace Cashier_Service
{
    public class PaymentsService
    {
        /// <summary>
        /// 查询收支
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Payments GetPayments(long id)
        {
            Payments payments = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    payments = new Payments();
                    payments.Id = id;
                    payments = new PaymentsDAO().GetPayments(conn, payments);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return payments;
        }

        /// <summary>
        /// 查询收支列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="enterpriseNo">企业业务号</param>
        /// <param name="storeNo">门店号</param>
        /// <param name="sortid">收支分类</param>
        /// <param name="type">类型</param>
        /// <param name="startdate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <returns></returns>
        public DataTable GetPaymentsTable(int pageindex, int pagesize, String enterpriseNo, String storeNo,
            int sortid, int type, String startdate, String enddate)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Payments payments = new Payments();
                    payments.Pageindex = pageindex;
                    payments.Pagesize = pagesize;
                    payments.EnterpriseNo = enterpriseNo;
                    payments.StoreNo = storeNo;
                    payments.PaymentsSortid = sortid;
                    payments.PaymentsType = type;
                    payments.StartDate = startdate;
                    payments.EndDate = enddate;
                    dt = new PaymentsDAO().GetPaymentsTable(conn, payments);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询收支记录数
        /// </summary>
        /// <param name="enterpriseNo">企业业务号</param>
        /// <param name="storeNo">门店号</param>
        /// <param name="sortid">收支分类</param>
        /// <param name="type">类型</param>
        /// <param name="startdate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <returns></returns>
        public long GetPaymentsRecord(String enterpriseNo, String storeNo, int sortid, int type, String startdate, String enddate)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Payments payments = new Payments();
                    payments.EnterpriseNo = enterpriseNo;
                    payments.StoreNo = storeNo;
                    payments.PaymentsSortid = sortid;
                    payments.PaymentsType = type;
                    payments.StartDate = startdate;
                    payments.EndDate = enddate;
                    record = new PaymentsDAO().GetPaymentsRecord(conn, payments);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 插入收支
        /// </summary>
        /// <param name="payments"></param>
        /// <returns></returns>
        public long InsertPayments(Payments payments)
        {
            long id = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    id = new PaymentsDAO().InsertPayments(conn, payments);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }

        /// <summary>
        /// 更新收支
        /// </summary>
        /// <param name="payments"></param>
        /// <returns></returns>
        public int UpdatePayments(Payments payments)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    flag = new PaymentsDAO().UpdatePayments(conn, payments);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 删除收支
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeletePayments(String ids)
        {
            int count = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    if (!String.IsNullOrEmpty(ids))
                    {
                        conn.Open();
                        String[] arr = ids.Split(new Char[] { ',' });
                        PaymentsDAO paymentsDAO = new PaymentsDAO();
                        Payments payments = new Payments();
                        foreach (String lopid in arr)
                        {
                            payments.Id = Int64.Parse(lopid);
                            count += paymentsDAO.DeletePayments(conn, payments);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }
    }
}
